package com.youmans.userservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.youmans.common_utils.exception.DfsException;
import com.youmans.common_utils.handler.JwtUtils;
import com.youmans.common_utils.handler.MD5;
import com.youmans.common_utils.vo.LoginVo;
import com.youmans.common_utils.vo.RegisterVo;
import com.youmans.common_utils.vo.UserForPic;
import com.youmans.userservice.entity.YoumansUser;
import com.youmans.userservice.mapper.YoumansUserMapper;
import com.youmans.userservice.service.YoumansUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youmans.userservice.utils.SaltUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author youmans
 * @since 2022-10-17
 */
@Service
@Primary
public class YoumansUserServiceImpl extends ServiceImpl<YoumansUserMapper, YoumansUser> implements YoumansUserService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate ;

    @Autowired
    private YoumansUserMapper youmansUserMapper ;
    @Override
    public List<UserForPic> list() {
        return youmansUserMapper.selectAllList();
    }

    @Override
    public String login(LoginVo loginVo) {
        String username = loginVo.getUsername();
        String password = loginVo.getPassword();
        String email = loginVo.getEmail();
        if(StringUtils.isEmpty(username) ||StringUtils.isEmpty(password)){
            throw new DfsException(20001,"登录失败，用户名或密码错误") ;
        }
//        判断查询对象是否为空
//        QueryWrapper<YoumansUser> wrapper = new QueryWrapper<>() ;
//        wrapper.eq("username",username) ;
//        YoumansUser youmansUser = baseMapper.selectOne(wrapper);
        YoumansUser youmansUser = youmansUserMapper.selectByName(username);
        if(youmansUser == null){

            throw new DfsException(20001,"登录失败,用户名不存在") ;
        }
        if(!MD5.encrypt(password).equals(youmansUser.getPassword())){

            throw new DfsException(20001,"登录失败，用户名或密码错误") ;
        }
        youmansUser.setPassword(null) ;
        if(youmansUser.getIsDeleted()==1){
            throw new DfsException(20001,"登录失败，账号禁用，请联系管理员") ;
        }

//        登录成功，生成token字符串，使用jwt工具
        return  JwtUtils.getJwtToken(youmansUser.getUserId().toString(),youmansUser.getUsername()) ;
    }

    @Override
    public boolean register(RegisterVo registerVo) {
        String username = registerVo.getUsername();
        String password = registerVo.getPassword();
        String code = registerVo.getCode();//c重复密码

        if(StringUtils.isEmpty(code)){
            throw new DfsException(20001,"注册失败，密码错误") ;
        }

        if(StringUtils.isEmpty(username)||StringUtils.isEmpty(password)){
            throw new DfsException(20001,"注册失败，空字符") ;
        }

        //String redisCode = redisTemplate.opsForValue().get("youmans_dfs:code:" + username);

        if(!code.equals(password)){
            throw new DfsException(20001,"注册失败，验证码错误") ;
        }
        //验证码一次性确认删除
       // redisTemplate.delete("youmans_dfs:code:" + username);

//        判断重复

        Integer count = youmansUserMapper.selectCountByUserName(username);
        if(username=="admin"||count > 0){
            throw new DfsException(20001,"该用户名已经注册请重新输入") ;
        }

        YoumansUser member = new YoumansUser() ;
        BeanUtils.copyProperties(registerVo,member);
//        member.setPassword(MD5.encrypt(password));
        member.setIsDeleted(0);
        member.setAvatar("https://gulimall-station.oss-cn-hangzhou.aliyuncs.com/2022-04-27/3892a1e224194ab0ae5df478f651ba43.png") ;
        //1.生成随机盐
        String salt = SaltUtils.getSalt(8);
        Md5Hash md5Hash = new Md5Hash(password,salt,1024);
        member.setPassword(md5Hash.toHex());
        //2.将随机盐保存到数据
        member.setSalt(salt);

        int insert = baseMapper.insert(member);
        return insert>0;
    }

    @Override
    public LoginVo getLoginInfo(String memberId) {
        YoumansUser youmansUser = baseMapper.selectById(memberId);
        LoginVo loginVo = new LoginVo();
        loginVo.setPassword(null);
        BeanUtils.copyProperties(youmansUser, loginVo);
//        if(StringUtils.isEmpty(loginVo.getAvatar())){
//            loginVo.setAvatar("https://gulimall-station.oss-cn-hangzhou.aliyuncs.com/2022-04-27/3892a1e224194ab0ae5df478f651ba43.png");
//        }
        return loginVo;
    }

    @Override
    public String getUserIdByname(String username) {
        LambdaQueryWrapper<YoumansUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(YoumansUser::getUserId) ;
        queryWrapper.eq(YoumansUser::getUsername,username) ;
        YoumansUser youmansUser = youmansUserMapper.selectOne(queryWrapper);
        return youmansUser.getUserId().toString();
    }

    @Override
    public String getIdByname(String username) {
        LambdaQueryWrapper<YoumansUser> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.select(YoumansUser::getUserId);
//        queryWrapper.select(YoumansUser::getUsername);
        queryWrapper.eq(YoumansUser::getUsername,username);
        YoumansUser youmansUser = baseMapper.selectOne(queryWrapper);
        if(StringUtils.isEmpty(youmansUser)){
            return null ;
        }
        return String.valueOf(youmansUser.getUserId());
    }
//获取所有的用户名
    @Override
    public List<String> getUserNames() {
        LambdaQueryWrapper<YoumansUser> queryWrapper =  new LambdaQueryWrapper<>();
        queryWrapper.select(YoumansUser::getUsername);
        List<YoumansUser> list = baseMapper.selectList(queryWrapper);
        List<String> names = new ArrayList<>();
        for (YoumansUser youmansUser : list) {
            names.add(youmansUser.getUsername());
        }
        return names;
    }

    @Override
    public YoumansUser selectInfoByName(String name) {
        return baseMapper.selectByName(name);
    }

    @Override
    public YoumansUser getUserInfoByName(String name) {
        LambdaQueryWrapper<YoumansUser> queryWrapper =  new LambdaQueryWrapper<>();
        queryWrapper.eq(YoumansUser::getUsername,name);
        return baseMapper.selectOne(queryWrapper);
    }
}
